import pygame
import sys
import random


class HeroPlane:
    """我方飞机类"""

    def __init__(self, screen):
        self.x = 325
        self.y = 435
        self.screen = screen
        self.image = pygame.image.load('./images/hero_plane.png')
        self.bullet_list = []

    def display(self):
        for bu in self.bullet_list:  # 遍历我方飞机子弹
            bu.display()
            if bu.move():
                self.bullet_list.remove(bu)  # 如果出屏幕 删除子弹对象
        self.screen.blit(self.image, (self.x, self.y))

    def move_left(self):
        self.x -= 10
        if self.x <= 0:
            self.x = 0

    def move_right(self):
        self.x += 10
        if self.x >= 650:
            self.x = 650

    def move_up(self):
        self.y -= 10
        if self.y <= 0:
            self.y = 0

    def move_down(self):
        self.y += 10
        if self.y >= 490:
            self.y = 490

    def fire(self):
        self.bullet_list.append(HeroBullet(self.screen, self.x, self.y))


class HeroBullet:
    """英雄子弹类"""

    def __init__(self, screen, x, y):
        self.x = x + 69
        self.y = y
        self.screen = screen
        self.image = pygame.image.load('./images/hero_bullet.png')

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))

    def move(self):
        self.y -= 5
        if self.y <= 0:  # 如果出屏幕
            return True


class EnemyPlane:
    """敌机类"""
    fen = 0

    def __init__(self, screen):
        self.x = random.randrange(651, step=10)
        self.y = -110
        self.screen = screen
        self.image = pygame.image.load('./images/enemy_plane.png')

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))

    def move(self, hero):
        self.y += 5

        if self.y >= 600:
            return True

        for bu in hero.bullet_list:  # 遍历我方飞机子弹
            if self.x + 5 < bu.x < self.x + 120 and self.y + 5 < bu.y < self.y + 150:
                EnemyPlane.fen += 1
                pygame.display.set_caption('Python简易飞机大战 --- 游戏得分：' + str(EnemyPlane.fen))
                print('-' * 30)
                print('摧毁1架敌机，当前游戏得分为：%d' % EnemyPlane.fen)
                return True

    def boom(self):

        for i in range(1, 6):
            self.image = pygame.image.load('./images/boom' + str(i) + '.png')
            self.screen.blit(self.image, (self.x, self.y))


class EnemyBullet:
    """敌机子弹类"""

    def __init__(self, screen, x, y):
        self.x = x + 70
        self.y = y
        self.screen = screen
        self.image = pygame.image.load('./images/hero_bullet.png')

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))

    def move(self):
        self.y += 10
        if self.y >= 600:
            return True


def game_over():
    """游戏结束函数"""
    print('游戏结束！')
    print('-' * 30)
    pygame.quit()
    sys.exit()


def key_control(hero):
    """用户按键函数"""
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            print('-' * 30)
            print('用户点击关闭按钮，', end='')
            game_over()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                print('-' * 30)
                print('用户按下Esc按键，', end='')
                game_over()

    pressed_keys = pygame.key.get_pressed()
    if pressed_keys[pygame.K_LEFT] or pressed_keys[pygame.K_a]:
        hero.move_left()
    elif pressed_keys[pygame.K_RIGHT] or pressed_keys[pygame.K_d]:
        hero.move_right()
    if pressed_keys[pygame.K_UP] or pressed_keys[pygame.K_w]:
        hero.move_up()
    elif pressed_keys[pygame.K_DOWN] or pressed_keys[pygame.K_s]:
        hero.move_down()
    if pressed_keys[pygame.K_SPACE]:
        hero.fire()


def main():
    """游戏主函数"""
    pygame.init()
    size = 800, 600
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption('Python简易飞机大战')
    fps_clock = pygame.time.Clock()
    bg = pygame.image.load('./images/bg.jpg')
    m = -600
    hero = HeroPlane(screen)
    enemy_list = []
    enemy_bullet_list = []

    while True:
        screen.blit(bg, (0, m))
        m += 2
        if m >= 0:
            m = -600
        hero.display()

        if random.randrange(80) == 10:  # 随机添加敌机到列表
            enemy_list.append(EnemyPlane(screen))

        for em_bu in enemy_bullet_list:  # 遍历敌方子弹
            em_bu.display()
            if hero.x + 8 < em_bu.x < hero.x + 128 and hero.y + 8 < em_bu.y < hero.y + 158:
                print('-' * 30)
                print('我方飞机被子弹打中', end='')
                game_over()
            if em_bu.move():
                enemy_bullet_list.remove(em_bu)

        for em in enemy_list:  # 遍历敌机
            em.display()
            if em.y - 80 < hero.y < em.y + 80 and em.x - 40 < hero.x < em.x + 40:
                print('-' * 30)
                print('我方飞机与敌方飞机发生肢体冲突', end='')
                game_over()
            elif em.x - 100 < hero.x < em.x + 100 and em.y - 65 < hero.y < em.y + 65:
                print('-' * 30)
                print('我方飞机与敌方飞机发生肢体冲突', end='')
                game_over()
            if em.move(hero):
                em.boom()
                enemy_list.remove(em)

            if random.randrange(50) == 10:  # 随机添加敌方子弹到列表
                enemy_bullet_list.append(EnemyBullet(screen, em.x, em.y))

        key_control(hero)
        pygame.display.update()
        fps_clock.tick(60)


if __name__ == '__main__':
    main()
